package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.RequestAuthorityBean;
import com.attendance.www.hr.beans.RequestAuthorityPageBean;
import com.attendance.www.hr.entitys.RequestAuthority;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IRequestAuthorityService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 产假设置
 * @author 张鑫
 * 2017-10-27
 *
 */
@Controller
@RequestMapping("/requestAuthority")
public class RequestAuthorityController {
	
	@Resource
	private IRequestAuthorityService requestAuthorityService;
	
	/**
	 * 新增产假
	 * @param request
	 * @param requestAuthorityBean
	 * @return
	 */
	@RequestMapping(value = "/saveRequestAuthority.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveRequestAuthority(HttpServletRequest request, @RequestBody RequestAuthorityBean requestAuthorityBean){
		if (requestAuthorityBean.getEmployeeId()==null
				||requestAuthorityBean.getRequestType()==null
				||requestAuthorityBean.getHolidayQuota()==null) {
			throw new ResultFaultException("0_00001");
		}
		if (requestAuthorityService.saveRequestAuthority(requestAuthorityBean)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00007");
	}
	
	/**
	 * 删除产假
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteRequestAuthority.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteRequestAuthority(HttpServletRequest request){
		String requestAuthorityId=request.getParameter("requestAuthorityId");
		
		if (StringUtils.isBlank(requestAuthorityId)) {
			throw new ResultFaultException("0_00001");
		}
		if (requestAuthorityService.deleteRequestAuthority(requestAuthorityId)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00009");
	}
	
	/**
	 * 修改产假额度
	 * @param request
	 * @param requestAuthorityBean
	 * @return
	 */
	@RequestMapping(value = "/updateRequestAuthority.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateRequestAuthority(HttpServletRequest request, @RequestBody RequestAuthorityBean requestAuthorityBean){
		if (requestAuthorityBean.getRequestAuthorityId()==null
				||requestAuthorityBean.getEmployeeId()==null
				||requestAuthorityBean.getRequestType()==null
				||requestAuthorityBean.getHolidayQuota()==null) {
			throw new ResultFaultException("0_00001");
		}
		if (requestAuthorityService.updateRequestAuthority(requestAuthorityBean)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00008");
	}
	
	/**
	 * 查询产假信息
	 * @param request
	 * @param requestAuthorityBean
	 * @return
	 */
	@RequestMapping(value = "/getRequestAuthorityList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRequestAuthorityList(HttpServletRequest request, @RequestBody RequestAuthorityBean requestAuthorityBean){
		if (requestAuthorityBean.getPageCount()==null
				||requestAuthorityBean.getPageNo()==null) {
			throw new ResultFaultException("0_00001");
		}
		int pageNo=requestAuthorityBean.getPageNo();
		int pageCount=requestAuthorityBean.getPageCount();
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		requestAuthorityBean.setStartNum(startNum);
		RequestAuthorityPageBean requestAuthorityPageBean=new RequestAuthorityPageBean();
		requestAuthorityPageBean = requestAuthorityService.getRequestAuthorityList(requestAuthorityBean);
		return ResultMap.success(requestAuthorityPageBean);
	}
	
	/**
	 * 查询产假额度
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getHolidayQuota.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getHolidayQuota(HttpServletRequest request){
		String regionType=request.getParameter("regionType");
		String maternity=request.getParameter("maternity");
		
		if (StringUtils.isBlank(regionType)||StringUtils.isBlank(maternity)) {
			throw new ResultFaultException("0_00001");
		}
		int holidayQuota=requestAuthorityService.getHolidayQuota(regionType, maternity);
		if (holidayQuota>=0) {
			return ResultMap.success(holidayQuota);
		}
		throw new ResultFaultException("0_00013");
	}
	
	/**
	 * 根据ID查询产假信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRequestAuthorityById.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRequestAuthorityById(HttpServletRequest request){
		String requestAuthorityId=request.getParameter("requestAuthorityId");
		if (StringUtils.isBlank(requestAuthorityId)) {
			throw new ResultFaultException("0_00001");
		}
		RequestAuthority requestAuthority=requestAuthorityService.getRequestAuthorityById(requestAuthorityId);
		if (requestAuthority!=null) {
			return ResultMap.success(requestAuthority);
		}
		throw new ResultFaultException("0_00013");
	}

}
